package remove;

public class Solution {
    public static int removeElement (int[] nums, int val) {
        int lastIndex = nums.length - 1;
        int tmp = 0;

        for (int i = 0; i < lastIndex + 1; i++) {
            if (nums[i] == val) {
                if (nums[lastIndex] != val) {
                    nums[i] = nums[lastIndex]   ;
                    nums[lastIndex] = 0;
                    lastIndex--;
                } else {
                    nums[lastIndex] = 0;
                    --lastIndex;
                    i--;
                }
            }
        }

        return ++lastIndex;
    }
}

class Ts {
    public static void main(String[] args) {
        int[] nums = new int[] {4,4,0,1,0,2};
        int ret = Solution.removeElement(nums, 0);
        System.out.println(ret);
    }
}
